using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using ESoft.DataAccess.Models;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Logging;

namespace ESoft.DataAccess.EfCore
{
    /// <summary>
    /// 
    /// </summary>
    public class DataContext : DbContext, IUnitOfWork, IDisposable
    {
        /// <summary>
        /// 初始化
        /// </summary>
        /// <param name="options"></param>
        public DataContext(DbContextOptions<DataContext> options) : base(options)
        {

        }

        /// <summary>
        /// 获取当前事务
        /// </summary>
        /// <returns></returns>
        ITransaction IUnitOfWork.GetCurrentTransaction()
        {
            return new DbTransaction(Database.CurrentTransaction);
        }

        /// <summary>
        /// 开启事务
        /// </summary>
        /// <returns></returns>
        public ITransaction BeginTransaction()
        {
            var currentTransaction = Database.BeginTransaction();
            return new DbTransaction(currentTransaction);
        }

        public void ExcuteTransaction(Action<ITransaction> action)
        {
            using (var transaction = BeginTransaction())
            {
                try
                {
                    action(transaction);
                    transaction.CommitTransaction();
                }
                catch (Exception e)
                {
                    transaction.RollbackTransaction();
                    throw e;
                }
                finally
                {
                    if (transaction != null)
                    {
                        transaction.Dispose();
                    }
                }
            }
        }

        /// <summary>
        /// 执行事务
        /// </summary>
        /// <param name="action"></param>
        /// <returns></returns>
        public async Task ExcuteTransactionAsync(Func<ITransaction, Task> action)
        {
            using (var transaction = BeginTransaction())
            {
                try
                {
                    await action(transaction);
                    transaction.CommitTransaction();
                }
                catch (Exception e)
                {
                    transaction.RollbackTransaction();
                    throw e;
                }
                finally
                {
                    if (transaction != null)
                    {
                        transaction.Dispose();
                    }
                }
            }
        }

        /// <summary>
        /// 提交任务
        /// </summary>
        /// <param name="transaction"></param>
        public void CommitTransaction(ITransaction transaction)
        {
            if (transaction == null) throw new ArgumentNullException(nameof(transaction));

            try
            {
                transaction.CommitTransaction();
            }
            catch
            {
                RollbackTransaction(transaction);
                throw;
            }
            finally
            {
                if (transaction != null)
                {
                    transaction.Dispose();
                }
            }
        }

        /// <summary>
        /// 回滚事务
        /// </summary>
        /// <param name="transaction"></param>
        public void RollbackTransaction(ITransaction transaction)
        {
            try
            {
                transaction?.RollbackTransaction();
            }
            finally
            {
                if (transaction != null)
                {
                    transaction.Dispose();
                }
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="modelBuilder"></param>
        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            ////注册实体配置信息
            ICollection<EntityBase> entityMappers = DatabaseInitializer.EntityMappers;
            foreach (EntityBase mapper in entityMappers)
            {
                modelBuilder.Entity(mapper.GetType());
              //  b.Entity<T>()
            }
        }
    }
}